package cn.ac.siom.query.action;

import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class TestIP {

    public  boolean isInRange(String network, String mask) {
        String[] networkips = network.split("\\.");
        int ipAddr = (Integer.parseInt(networkips[0]) << 24)
                | (Integer.parseInt(networkips[1]) << 16)
                | (Integer.parseInt(networkips[2]) << 8)
                | Integer.parseInt(networkips[3]);
        //System.out.println(ipAddr);
        int type = Integer.parseInt(mask.replaceAll(".*/", ""));
        int mask1 = 0xFFFFFFFF << (32 - type);
        //System.out.println(mask1);
        String maskIp = mask.replaceAll("/.*", "");
        System.out.println(maskIp);
        String[] maskIps = maskIp.split("\\.");
        int cidrIpAddr = (Integer.parseInt(maskIps[0]) << 24)
                | (Integer.parseInt(maskIps[1]) << 16)
                | (Integer.parseInt(maskIps[2]) << 8)
                | Integer.parseInt(maskIps[3]);
        //System.out.println(cidrIpAddr);
        return (ipAddr & mask1) == (cidrIpAddr & mask1);
    }

        /**
         * 获取Ip地址
         * @param request
         * @return
         */
        public static String getIpAdrress(HttpServletRequest request) {
            String Xip = request.getHeader("X-Real-IP");
            String XFor = request.getHeader("X-Forwarded-For");
            if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
                //多次反向代理后会有多个ip值，第一个ip才是真实ip
                int index = XFor.indexOf(",");
                if(index != -1){
                    return XFor.substring(0,index);
                }else{
                    return XFor;
                }
            }
            XFor = Xip;
            if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
                return XFor;
            }
            if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
                XFor = request.getHeader("Proxy-Client-IP");
            }
            if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
                XFor = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
                XFor = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
                XFor = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
                XFor = request.getRemoteAddr();
            }
            return XFor;
        }

    /**public static void main(String[] args) {

        System.out.println(isInRange("210.72.9.1", "210.72.9.1/254"));
        System.out.println(isInRange("159.226.130.3", "159.226.130.1/254"));
        System.out.println(isInRange("210.72.70.1", "210.72.70.1/254"));
        System.out.println(isInRange("119.78.237.1", "119.78.237.1/254"));
        System.out.println(isInRange("119.78.238.1", "119.78.238.1/254"));
        System.out.println(isInRange("119.78.239.1", "119.78.239.1/254"));
        System.out.println(isInRange("172.16.231.1", "172.16.231.1/254"));
        System.out.println(isInRange("192.168.1.1", "192.168.1.0/254"));
        System.out.println(isInRange("192.168.0.4", "192.168.0.1/254"));


        System.out.println(isInRange("10.153.48.127", "10.153.48.0/26"));
        System.out.println(isInRange("10.168.1.2", "10.168.0.224/23"));
        System.out.println(isInRange("192.168.0.1", "192.168.0.0/24"));
        System.out.println(isInRange("10.168.0.0", "10.168.0.0/32"));




    } **/
}